﻿using System;
using System.IO;
using Microsoft.DirectX.Direct3D;
using Microsoft.DirectX.DirectSound;

namespace WiiShooter
{
    class Weapon
    {
        private int maxAmmo;
        public int ammo;
        private int fireRate;
        private string projectileType;
        private int clipSize;
        private Texture texture;
        public Texture weaponTexture;
        private Microsoft.DirectX.Direct3D.Device device;
        private Microsoft.DirectX.DirectSound.Device soundDevice;
        //Create and setup the buffer description.
        private BufferDescription buffer_desc = new BufferDescription();
        public SecondaryBuffer buffer;
        private String soundEffectName;


        public Weapon(String textFile, ref Microsoft.DirectX.Direct3D.Device devicePass, 
            ref Microsoft.DirectX.DirectSound.Device soundDevicePass)
        {
            device = devicePass;
            soundDevice = soundDevicePass;
            //initialize sound
            buffer_desc.ControlEffects = true; //this has to be true to use effects.
            buffer_desc.GlobalFocus = true; //play sound even if application loses focus.
            readFile(textFile);
        }

        /**
         * Read in all of the data for this weapon
         */ 
        private void readFile(String textFile)
        {
            StreamReader objReader = new StreamReader(textFile);
            string sLine = "";

            // Start read ye olde texte file(e).
            while (sLine != null)
            {
                sLine = objReader.ReadLine();
                if (sLine != null)
                {
                    if (sLine.StartsWith("Sprite"))
                    {
                        string[] inputs = sLine.Split(',');
                        texture = TextureLoader.FromFile(device, inputs[1]);
                    }
                    else if (sLine.StartsWith("WeaponSprite"))
                    {
                        string[] inputs = sLine.Split(',');
                        weaponTexture = TextureLoader.FromFile(device, inputs[1]);
                    }
                    else if (sLine.StartsWith("MaxAmmo"))
                    {
                        string[] inputs = sLine.Split(',');
                        maxAmmo = Int16.Parse(inputs[1]);
                    }
                    else if (sLine.StartsWith("StartAmmo"))
                    {
                        string[] inputs = sLine.Split(',');
                        ammo = Int16.Parse(inputs[1]);
                    }
                    else if (sLine.StartsWith("FireRate"))
                    {
                        string[] inputs = sLine.Split(',');
                        fireRate = Int16.Parse(inputs[1]);
                    }
                    else if (sLine.StartsWith("ClipSize"))
                    {
                        string[] inputs = sLine.Split(',');
                        clipSize = Int16.Parse(inputs[1]);
                    }
                    else if (sLine.StartsWith("Projectile"))
                    {
                        string[] inputs = sLine.Split(',');
                        projectileType = inputs[1];
                    }
                    else if (sLine.StartsWith("Sound"))
                    {
                        string[] inputs = sLine.Split(',');
                        soundEffectName = inputs[1];
                    }
                }
            }
            loadSounds();
        }

        /**
         * Fire a projectile.  We must check if we have enough ammo, if the clip is not empty,
         * and make sure we fire at the rate specified.
         */ 
        public void fire()
        {
            if (ammo > 0)
            {
                Projectile fireProjectile = new Projectile(Game.player, projectileType, ref device,
                    ref soundDevice);
                ammo--;
                //Console.WriteLine("Ammo left: " + ammo);
                //buffer.Play(0, BufferPlayFlags.Default);
                Game.projectileList.Add(fireProjectile);
            }
        }

        /**
         * Load a new clip, if we have enough ammo to do so
         */ 
        public void reload()
        {

        }

        private void loadSounds()
        {
            buffer = new SecondaryBuffer(soundEffectName, buffer_desc, soundDevice);
        }
    }
}
